const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
const util = require('../../utils/util.js');
const WxParse = require('../Common/wxParse/wxParse.js');
Page({
  data: {
    current:0,
    top: -1,
    loading: false,
    tripstop:1000,
    detailtop:1000,
    costtop:1000,
    tabstop:242,
    isfixed:false,
    isshare:false,
    tabheight:0,
    bottomLift: 0,
    id:'',
    infodata:'',
    Shareimg:'',
    content:'',
    schedule:'',
    expense:'',
    boxscroll:0,
    tabsindex:1,
    tabsbool:false,
    ishow:true,
  },
  onLoad(option) {
    let {bottomLift} = app.globalData
    if(bottomLift>=0){
      app.getDeviceSize().then(res => {
        const {bottomLift} = res
        this.setData({
          bottomLift
        })
      })
    }else{
      this.setData({
        bottomLift
      })
    }
    if(option.id){
      this.setData({
        id:option.id
      })
      this.activitydetail()
    }
    if(option.parentid){
      app.globalData.parentid=option.parentid
    }

    if(option.scene){
      var attr=decodeURIComponent(option.scene).split('&');
      this.setData({
        id:attr[0].split('id=')[1]
      })
      this.activitydetail()
    }

    if(option.scene){
      var attr=decodeURIComponent(option.scene).split('&');
      if(attr[1]){
        app.globalData.parentid=attr[1].split('parentid=')[1];
      }
    }

  },
  onShow(){
    if(wx.getStorageSync('logintoken')){
      this.info()
    }
  },
  onReady(){
    wx.pageScrollTo({
      scrollTop: 0 ,
      duration: 0  //滚动动画的时长，单位 ms
    })
    this.geheight();
  },
  info(){
    Controller.info().then(res=>{
      if(res.data.baseData){
        this.setData({
          infodata:res.data.baseData
        })
      }
    })
  },
  makeShare(){
    var that=this;
    // wx.showLoading({
    //   title: '海报生成中',
    //   mask: true,
    // })
    let userInfo=wx.getStorageSync('userInfo') || '';
    var path=app.getCurrentPageUrlWithArgs();
    var path1="pages/detail/detail";
    var scene='id='+this.data.id+'&parentid='+userInfo.id;

    Controller.makeShare({activityId:that.data.id,activityTimeId:that.data.detail.activityTimeId,path:path1,scene:(scene)}).then(res=>{
      if(res.data.baseData){
        that.setData({
          Shareimg:res.data.baseData,
        })
      }
      // wx.hideLoading();
    }).catch(res=>{
      // wx.hideLoading();
    })
  },
  saveImgBtn() {
    setTimeout(()=>{
      app.saveImg(this.data.Shareimg)
    },800)
  },

  activitydetail(){
    Controller.activitydetail({activityId:this.data.id}).then(res=>{
      if(res.data.baseData){
        if(res.data.baseData.content){
          var content=res.data.baseData.content
          var that = this;
          WxParse.wxParse('content', 'html', content, that, 5);
          res.data.baseData.content = JSON.stringify(res.data.baseData.content).replace(/<img/gi, "<img class='richImg' style='width:auto!important;height:auto!important;max-height:100%;width:100%;'");
          res.data.baseData.content = JSON.parse(res.data.baseData.content);
        }
        if(res.data.baseData.expense){
          var expense=res.data.baseData.expense
          var that = this;
          WxParse.wxParse('expense', 'html', expense, that, 5);
          res.data.baseData.expense = JSON.stringify(res.data.baseData.expense).replace(/<img/gi, "<img class='richImg' style='width:auto!important;height:auto!important;max-height:100%;width:100%;'");
          res.data.baseData.expense = JSON.parse(res.data.baseData.expense);
        }
        if(res.data.baseData.schedule){
          var schedule=res.data.baseData.schedule
          var that = this;
          WxParse.wxParse('schedule', 'html', schedule, that, 5);
          res.data.baseData.schedule = JSON.stringify(res.data.baseData.schedule).replace(/<img/gi, "<img class='richImg' style='width:auto!important;height:auto!important;max-height:100%;width:100%;'");
          res.data.baseData.schedule = JSON.parse(res.data.baseData.schedule);
        }
        this.setData({
          detail:res.data.baseData
        })
        var that=this;
        setTimeout(()=>{
          that.geheight();
        },300)
        // if(wx.getStorageSync('logintoken')){
        //   this.makeShare()
        // }
      }
    })
  },
  tabs: function (e) {
    var index = e.currentTarget.dataset.index
    this.setData({
      current: index
    })

  },
  share(){
    if(!wx.getStorageSync('userInfo')){
      app.ToLogin(false)
      return
    }
    this.makeShare()
    var that=this;
    setTimeout(()=>{
      that.setData({
        ishow:false,
        isshare:true
      },300)
    })
    
  },
  cancel(e){
    console.log(e)
    this.setData({
      ishow:e.detail
    })
  },
  geheight(){
    var query = wx.createSelectorQuery()
    var that=this;
    query.select('#tabstop').boundingClientRect(function (res) {
      that.setData({
        tabstop: res.top,
        tabheight:res.height
      })
    }).exec()
    query.select('.main').boundingClientRect(function (res) {
      that.setData({
        mainstop: res.top,
      })
    }).exec()

    //获取板块一离顶部的距离
    query.select('#detail').boundingClientRect(function (res) {
      that.setData({
        detailtop: res.top-that.data.tabheight-that.data.mainstop
      })

    }).exec()
    //获取板块二离顶部的距离
    query.select('#cost').boundingClientRect(function (res) {
      that.setData({
        costtop: res.top-that.data.tabheight-that.data.mainstop
      })
    }).exec()
    //获取板块三离顶部的距离
    query.select('#trips').boundingClientRect(function (res) {
      that.setData({
        tripstop: res.top-that.data.tabheight-that.data.mainstop
      })
    }).exec()
    
  },
  scroll(e){
    console.log(e)
    this.setData({
      boxscroll:e.detail.scrollTop
    })
    if(!this.data.tabsbool){
      var index=1;
      if(this.data.boxscroll<this.data.tripstop){
        index=1
      }else if(this.data.boxscroll>=this.data.tripstop-20 && this.data.boxscroll<=this.data.costtop){
        index=2
      }else{
        index=3
      }
      this.setData({
        tabsindex:index,
      })
    }
  },
  // 监听滚动事件
  onPageScroll: function (e) {
    // 滚动条当前位置
    let scrollTop = e.scrollTop
    this.setData({
      top: scrollTop
    })
    // if(this.data.top>=this.data.tabstop){
    //   this.setData({
    //     isfixed:true
    //   })
    // }else{
    //   this.setData({
    //     isfixed:false
    //   })
    // }
  },
  tabs2(e){
    var name=e.currentTarget.dataset.name;
    var index=e.currentTarget.dataset.index;
    this.setData({
      toview:name,
      tabsindex:index,
      tabsbool:true
    })
    var that=this;
    setTimeout(res=>{
      that.setData({
        tabsbool:false
      })
    },1000)
    wx.pageScrollTo({
      boxscroll:0,
      scrollTop: this.data.mainstop,
      duration: 300  //滚动动画的时长，单位 ms
    })
    // wx.pageScrollTo({
    //   scrollTop: this.data[name+'top'] ,
    //   duration: 300  //滚动动画的时长，单位 ms
    // })
    
  },
  golink(e){
    if(e.currentTarget.dataset.type=="swith"){
      wx.switchTab({
        url:e.currentTarget.dataset.url
      });
    }else{
      wx.navigateTo({
        url:e.currentTarget.dataset.url
      });
    }
    
  },
  buy(){
    if(!wx.getStorageSync('userInfo')){
      app.ToLogin(false)
      return
    }
    wx.navigateTo({
      url:"/pages/detail/calendar?id="+this.data.id
    });
  },
  onShareAppMessage: function (e) {
    let title = this.data.detail.title || '';
    let imageUrl = '';
    let userInfo=this.data.infodata || '';
    let path="/pages/detail/detail?id="+this.data.id+"&parentid="+(userInfo.id || '')
    console.log(path)
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {
        console.log('点击页面内的分享按钮--onShareAppMessage分享成功', res)
      },
      fail(err) {
        console.log('点击页面内的分享按钮--onShareAppMessage-fail', err)
      }
    }
    return obj
  },
  onShareTimeline: function(res){
    let title = this.data.detail.title || '';
    let imageUrl = '';
    let userInfo=this.data.infodata || '';
    let path="/pages/detail/detail?id="+this.data.id+"&parentid="+(userInfo.id || '')

    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {
        console.log('点击页面内的分享按钮--onShareAppMessage分享成功', res)
      },
      fail(err) {
        console.log('点击页面内的分享按钮--onShareAppMessage-fail', err)
      }
    }
    return obj
  },
})